/**
 * 
 */
package com.lanswon.qzsmk.controller.appManageController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.AppClassification;
import com.lanswon.qzsmk.model.AppOpenReg;
import com.lanswon.qzsmk.model.BikeSection;
import com.lanswon.qzsmk.service.appManageService.AppOpenRegService;
import com.lanswon.qzsmk.service.preService.BikeService;
import com.lanswon.qzsmk.vo.AppOpenRegVo;

import ch.qos.logback.classic.Logger;

/**
 * @2018/3/15 dy
 *
 */
@Controller
@RequestMapping(value = "/appOpenReg")
public class AppOpenRegController {

	@Autowired
	// 卡应用开通映射
	AppOpenRegService ac;
	
	@Autowired
	BikeService bikeService;
	
	private final static Logger logger = (Logger) LoggerFactory.getLogger(AppOpenRegController.class);
	
	/**
	 * 跳转到应用开通页面
	 * @return
	 */
	@RequiresPermissions("appOpenReg:index")
	@RequestMapping(name = "应用开通关闭主页,appOpenReg:index",value = "/toAppOpenInfoPage", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryapplicationopeningstop() {
		return "AppOpenInfo";
	}
	
	/**
	 * 查询卡片应用刚开通情况
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:getAppOpenInfo")
	@RequestMapping(name="查询卡片应用开通关闭信息,appOpenReg:getAppOpenInfo",value="/getAppOpenInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getAppOpenInfo(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map = ac.queryAppOpenReg(request);
		return map;
	}
	
	/**
	 * 请求写卡数据
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:appOpenToWriteCard")
	@RequestMapping(name="请求写卡数据,appOpenReg:appOpenToWriteCard",value="/appOpenToWriteCard",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> appOpenToWriteCard(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map = ac.openAppRegister(request);
		HttpSession session=request.getSession();
		session.setAttribute("vipnoByOpenApp", map.get("vipno"));
		session.setAttribute("vipcardsnByOpenApp", map.get("vipcardsn"));
		logger.debug("cardsn==>"+ map.get("cardsn"));
		session.setAttribute("cardsnByOpenApp", map.get("cardsn"));
		return map;
	}
	
	/**
	 * 开通自行车应用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:openBikeAppRegister")
	@RequestMapping(name="自行车应用开通,appOpenReg:openBikeAppRegister",value="/openBikeAppRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> openBikeAppRegister(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		HttpSession session=request.getSession();
		String vipno = (String) session.getAttribute("vipnoByOpenApp");
		String vipcardsn = (String) session.getAttribute("vipcardsnByOpenApp");
		String cardsn = (String) session.getAttribute("cardsnByOpenApp");
		logger.debug("sscardsn==>"+cardsn);
		session.removeAttribute("vipnoByOpenApp");
		session.removeAttribute("vipcardsnByOpenApp");
		session.removeAttribute("cardsnByOpenApp");
		Map<String, Object> resultMap = ac.executeOpenAppReg(request,vipno,vipcardsn,cardsn);
		logger.debug("resultMap==>"+resultMap);
		request.getSession().setAttribute("vo", resultMap.get("certVo"));
		request.getSession().setAttribute("type", resultMap.get("type"));
		map.put("max", "true");
        map.put("title",resultMap.get("title") );
        map.put("rel", "openBikeApp");
		map.put("statusCode", "200");
		map.put("jumpToTabid", "AppOenInfo");
		map.put("jumpToUrl", "appOpenReg/toAppOpenInfoPage");
		map.put("jumpToTile", "应用开通停用");
		map.put("url", "printCredentials/toHairpinPdfView");
		
		

		logger.debug("map==>"+map);
		return map;
	}
	
	/**
	 * 关闭自行车应用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:closeBikeAppRegister")
	@RequestMapping(name="关闭自行车应用,appOpenReg:closeBikeAppRegister",value="/closeBikeAppRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> closeBikeAppRegister(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> resultMap = ac.executeCloseBikeAppReg(request);
		logger.debug("resultMap==>"+resultMap);
		request.getSession().setAttribute("vo", resultMap.get("certVo"));
		request.getSession().setAttribute("type", resultMap.get("type"));
		map.put("max", "true");
        map.put("title",resultMap.get("title"));
        map.put("rel", "closeBikeApp");
		map.put("statusCode", "200");
		map.put("appFlag", resultMap.get("appFlag"));
		map.put("citizenCardNo", resultMap.get("citizenCardNo"));
		map.put("jumpToTabid", "AppOenInfo");
		map.put("jumpToUrl", "appOpenReg/toAppOpenInfoPage");
		map.put("jumpToTile", "应用开通停用");
		map.put("url", "printCredentials/toHairpinPdfView");
		
		logger.debug("map==>"+map);
		return map;
	}
	
	/**
	 * 开通公交应用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:openBusAppRegister")
	@RequestMapping(name="开通公交应用,appOpenReg:openBusAppRegister",value="/openBusAppRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> openBusAppRegister(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> resultMap = ac.executeOpenBusAppReg(request);
		logger.debug("resultMap==>"+resultMap);
		request.getSession().setAttribute("vo", resultMap.get("certVo"));
		request.getSession().setAttribute("type", resultMap.get("type"));
		map.put("max", "true");
        map.put("title",resultMap.get("title") );
        map.put("rel", "openBusApp");
		map.put("statusCode", "200");
		map.put("url", "printCredentials/toHairpinPdfView");
		
		map.put("jumpToTabid", "AppOenInfo");
		map.put("jumpToUrl", "appOpenReg/toAppOpenInfoPage");
		map.put("jumpToTile", "应用开通停用");
		logger.debug("map==>"+map);
		return map;
	}
	
	/**
	 * 关闭公交应用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:closeBusAppRegister")
	@RequestMapping(name="关闭公交应用,appOpenReg:closeBusAppRegister",value="/closeBusAppRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> closeBusAppRegister(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> resultMap = ac.executeCloseBusAppReg(request);
		logger.debug("resultMap==>"+resultMap);
		request.getSession().setAttribute("vo", resultMap.get("certVo"));
		request.getSession().setAttribute("type", resultMap.get("type"));
		map.put("max", "true");
        map.put("title",resultMap.get("title") );
        map.put("rel", "closeBusApp");
		map.put("statusCode", "200");
		map.put("url", "printCredentials/toHairpinPdfView");
		
		map.put("jumpToTabid", "AppOenInfo");
		map.put("jumpToUrl", "appOpenReg/toAppOpenInfoPage");
		map.put("jumpToTile", "应用开通停用");
		logger.debug("map==>"+map);
		return map;
	}
	
	/**
	 * 通过市民卡号查询是卡个人信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:queryCardInfoByCardNo")
	@RequestMapping(name="查询卡片信息,appOpenReg:queryCardInfoByCardNo",value="/queryCardInfoByCardNo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryCardInfoByCardNo(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map = ac.queryCardInfo(request);
		return map;
	}
	
	/**
	 * 通过市民卡号查询密钥
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:getBikeAppPWInfo")
	@RequestMapping(name="查询密钥,appOpenReg:getBikeAppPWInfo",value="/getBikeAppPWInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getBikeAppPWInfo(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map = ac.queryBikeAppPwdInfoByCitizenNo(request);
		map.put("statusCode", "200");
		return map;
	}
	
	
	@ResponseBody
	@RequiresPermissions("appOpenReg:getOverrideInfo")
	@RequestMapping(value="/getOverrideInfo",name="获取自行车重写数据和密钥,appOpenReg:getOverrideInfo",method=RequestMethod.POST)
	public Map<String, Object>getOverrideInfo(String cardsn,String citizenCardNo) throws ApplicationException{
		Map<String, Object> map=new HashMap<>();
		JSONArray jsonArray=bikeService.bikeAppRewriteCardData(cardsn, citizenCardNo);//写卡数据
		List<BikeSection> list=bikeService.queryBikeSection(citizenCardNo, cardsn);//扇区密钥
		map.put("jsonArray", jsonArray);
		map.put("key", list);
		return map;
	}
	
	/**
	 * 开通图书馆应用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:openLibAppRegister") 
	@RequestMapping(name="图书馆应用开通,appOpenReg:openLibAppRegister",value="/openLibAppRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> openLibAppRegister(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> resultMap = ac.executeOpenLibAppReg(request);
		logger.debug("resultMap==>"+resultMap);
		request.getSession().setAttribute("vo", resultMap.get("certVo"));
		request.getSession().setAttribute("type", resultMap.get("type"));
		map.put("max", "true");
        map.put("title",resultMap.get("title") );
        map.put("rel", "openBusApp");
		map.put("statusCode", "200");
		map.put("url", "printCredentials/toHairpinPdfView");
		
		map.put("jumpToTabid", "AppOenInfo");
		map.put("jumpToUrl", "appOpenReg/toAppOpenInfoPage");
		map.put("jumpToTile", "应用开通停用");
		logger.debug("map==>"+map);
		return map;
	}
	
	/**
	 * 关闭图书馆应用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("appOpenReg:closeLibAppRegister")
	@RequestMapping(name="关闭图书馆应用,appOpenReg:closeLibAppRegister",value="/closeLibAppRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> closeLibAppRegister(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> resultMap = ac.executeCloseLibAppReg(request);
		logger.debug("resultMap==>"+resultMap);
		request.getSession().setAttribute("vo", resultMap.get("certVo"));
		request.getSession().setAttribute("type", resultMap.get("type"));
		map.put("max", "true");
        map.put("title",resultMap.get("title") );
        map.put("rel", "closeLibApp");
		map.put("statusCode", "200");
		map.put("url", "printCredentials/toHairpinPdfView");
		
		map.put("jumpToTabid", "AppOenInfo");
		map.put("jumpToUrl", "appOpenReg/toAppOpenInfoPage");
		map.put("jumpToTile", "应用开通停用");
		logger.debug("map==>"+map);
		return map;
	}
}


